package cn.kgc.spring03.entity;

import org.gjt.mm.mysql.Driver;
import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author: mosin
 * @version: v1.0  2022/11/30
 * 连接的工厂对象  将该工厂对象托管在spring容器中可以获取连接对象
 */
public class ConnectionFactoryBean implements FactoryBean<Connection> {
    // 该类在spring容器中获取的实际对象
    @Override
    public Connection getObject() throws Exception {

        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql:///java2217?serverTimezone=UTC";
        String username = "root";
        String pwd = "root";
        Connection connection = DriverManager.getConnection(url, username, pwd);
        return connection;
    }

    //  返回对象的类型
    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }

    // 设置该类返回的对象是否是单例的
    @Override
    public boolean isSingleton() {
        return false;
    }
}
